package com.kai.steering.data.service.ctl;

import com.baomidou.mybatisplus.extension.api.R;
import com.kai.steering.data.service.entity.domainObject.NoteDO;
import com.kai.steering.data.service.entity.po.Note;
import com.kai.steering.data.service.entity.vo.NoteVO;
import com.kai.steering.data.service.service.NoteService;
import com.kai.steering.data.service.entity.dto.NoteQueryDTO;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import com.kai.steering.data.service.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.vo.Page;
import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.List;


/**
 * <pre>
 * 笔记 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/note")
@Api(tags = "笔记 API")
public class NoteController extends BaseController {
    @Autowired
    private NoteService noteService;

    @PostMapping
    @ApiOperation(value = "笔记-添加")
    public Result<Boolean> addNote(@Valid @RequestBody NoteQueryDTO noteQueryDTO) {

        /**
         * 参数检查
         */
        if (StringUtils.isEmpty(noteQueryDTO.getUserQuestionId())) {
            return Result.failed("笔记关联题目id为空");
        }


        //构建新对象
        NoteDO newObj = new NoteDO(noteQueryDTO);

        boolean flag =noteService.saveNote(newObj);
        return Result.ok(flag);
    }


    @PutMapping("/{noteId}")
    @ApiOperation(value = "笔记-编辑")
    public Result<Boolean> updateNote(@PathVariable("noteId") Serializable noteId, @Valid @RequestBody NoteQueryDTO noteQueryDTO) {
        /**
         * 参数验证
         * 笔记中id与用户题目id相互对应
         */
        //id
        if (StringUtils.isEmpty(noteId.toString())) {
            return Result.failed("noteId为空");
        }

        /**
         * 构建对象
         */
        NoteDO noteDO = new NoteDO(noteQueryDTO);
        noteDO.setNoteId((String) noteId);
        /**
         * 调用服务
         */
        boolean noteDOResult = noteService.updateNote(noteId,noteDO);

        return Result.ok(noteDOResult);
    }


//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "笔记-删除")
//    public Result<Boolean> deleteNote(@PathVariable("id") Long id) {
//
//        boolean flag =false;
//        return Result.ok(flag);
//    }


    @GetMapping("/{noteId}")
    @ApiOperation(value = "笔记-详情")
    public Result<NoteVO> getNote(@PathVariable("noteId") Serializable noteId) {
        /**
         * 参数校验
         */
        if (StringUtils.isEmpty(noteId.toString())) {
            return Result.failed("noteId为空");
        }

        /**
         * 获取
         */
        NoteDO noteDO = noteService.getNoteById(noteId);

        /**
         * 转换
         */
        if (noteDO==null|| StringUtils.isEmpty(noteDO.getNoteId())) {
            Result.failed();
        }
        NoteVO noteVO = new NoteVO(noteDO);
        return Result.ok(noteVO);
    }


//    @PostMapping("/list")
//    @ApiOperation(value = "笔记-综合查询")
//    public Result<Page<NoteVO>> getNotePageList(@Valid @RequestBody NoteQueryDTO noteQueryDTO) {
//        Page<NoteVO> paging = null;
//        return Result.ok(paging);
//    }


//    @GetMapping("/list")
//    @ApiOperation(value = "笔记-全部列表")
//    public Result<List<NoteVO>> getNoteAllList() {
//
//        List<NoteVO> list = null;
//        return Result.ok(list);
//    }


}